home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / build_scripts.py < prev    next >
Text File  |  2005-11-19  |  4KB  |  129 lines

  1. """distutils.command.build_scripts
  2.  
  3. Implements the Distutils 'build_scripts' command."""
  4.  
  5. # This module should be kept compatible with Python 1.5.2.
  6.  
  7. __revision__ = "$Id: build_scripts.py,v 1.21.8.1 2004/04/01 03:56:46 fdrake Exp $"
  8.  
  9. import sys, os, re
  10. from stat import ST_MODE
  11. from distutils import sysconfig
  12. from distutils.core import Command
  13. from distutils.dep_util import newer
  14. from distutils.util import convert_path
  15. from distutils import log
  16.  
  17. # check if Python is called on the first line with this expression
  18. first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
  19.  
  20. class build_scripts (Command):
  21.  
  22.     description = "\"build\" scripts (copy and fixup #! line)"
  23.  
  24.     user_options = [
  25.         ('build-dir=', 'd', "directory to \"build\" (copy) to"),
  26.         ('force', 'f', "forcibly build everything (ignore file timestamps"),
  27.         ]
  28.  
  29.     boolean_options = ['force']
  30.  
  31.  
  32.     def initialize_options (self):
  33.         self.build_dir = None
  34.         self.scripts = None
  35.         self.force = None
  36.         self.outfiles = None
  37.  
  38.     def finalize_options (self):
  39.         self.set_undefined_options('build',
  40.                                    ('build_scripts', 'build_dir'),
  41.                                    ('force', 'force'))
  42.         self.scripts = self.distribution.scripts
  43.  
  44.     def get_source_files(self):
  45.         return self.scripts
  46.  
  47.     def run (self):
  48.         if not self.scripts:
  49.             return
  50.         self.copy_scripts()
  51.  
  52.  
  53.     def copy_scripts (self):
  54.         """Copy each script listed in 'self.scripts'; if it's marked as a
  55.         Python script in the Unix way (first line matches 'first_line_re',
  56.         ie. starts with "\#!" and contains "python"), then adjust the first
  57.         line to refer to the current Python interpreter as we copy.
  58.         """
  59.         self.mkpath(self.build_dir)
  60.         outfiles = []
  61.         for script in self.scripts:
  62.             adjust = 0
  63.             script = convert_path(script)
  64.             outfile = os.path.join(self.build_dir, os.path.basename(script))
  65.             outfiles.append(outfile)
  66.  
  67.             if not self.force and not newer(script, outfile):
  68.                 log.debug("not copying %s (up-to-date)", script)
  69.                 continue
  70.  
  71.             # Always open the file, but ignore failures in dry-run mode --
  72.             # that way, we'll get accurate feedback if we can read the
  73.             # script.
  74.             try:
  75.                 f = open(script, "r")
  76.             except IOError:
  77.                 if not self.dry_run:
  78.                     raise
  79.                 f = None
  80.             else:
  81.                 first_line = f.readline()
  82.                 if not first_line:
  83.                     self.warn("%s is an empty file (skipping)" % script)
  84.                     continue
  85.  
  86.                 match = first_line_re.match(first_line)
  87.                 if match:
  88.                     adjust = 1
  89.                     post_interp = match.group(1) or ''
  90.  
  91.             if adjust:
  92.                 log.info("copying and adjusting %s -> %s", script,
  93.                          self.build_dir)
  94.                 if not self.dry_run:
  95.                     outf = open(outfile, "w")
  96.                     if not sysconfig.python_build:
  97.                         outf.write("#!%s%s\n" % 
  98.                                    (os.path.normpath(sys.executable),
  99.                                     post_interp))
  100.                     else:
  101.                         outf.write("#!%s%s\n" %
  102.                                    (os.path.join(
  103.                             sysconfig.get_config_var("BINDIR"),
  104.                             "python" + sysconfig.get_config_var("EXE")),
  105.                                     post_interp))
  106.                     outf.writelines(f.readlines())
  107.                     outf.close()
  108.                 if f:
  109.                     f.close()
  110.             else:
  111.                 f.close()
  112.                 self.copy_file(script, outfile)
  113.  
  114.         if os.name == 'posix':
  115.             for file in outfiles:
  116.                 if self.dry_run:
  117.                     log.info("changing mode of %s", file)
  118.                 else:
  119.                     oldmode = os.stat(file)[ST_MODE] & 07777
  120.                     newmode = (oldmode | 0555) & 07777
  121.                     if newmode != oldmode:
  122.                         log.info("changing mode of %s from %o to %o",
  123.                                  file, oldmode, newmode)
  124.                         os.chmod(file, newmode)
  125.  
  126.     # copy_scripts ()
  127.  
  128. # class build_scripts
  129.